<template>
  <Dropdown placement="bottomLeft"
            :overlayClassName="`${prefixCls}-dropdown-overlay`">
    <span :class="[prefixCls, `${prefixCls}--${theme}`]"
          class="flex">
      <img :class="`${prefixCls}__header`"
           :src="getUserInfoPhoto" />
      <span :class="`${prefixCls}__info hidden md:block`">
        <span :class="`${prefixCls}__name  `"
              class="truncate">
          {{ getUserInfo.userName }}
        </span>
      </span>
    </span>

    <template #overlay>
      <!-- @click="handleMenuClick" -->
      <Menu>
        <MenuItem key="edit"
                  @click="editPhoto"
                  :text="t('layout.header.modifyThePicture')"
                  icon="ant-design:meh-outlined" />
        <MenuDivider v-if="getShowDoc" />
        <MenuItem key="lock"
                  @click="handleLock"
                  :text="t('layout.header.tooltipLock')"
                  icon="ion:lock-closed-outline" />
        <MenuItem key="logout"
                  @click="handleLoginOut"
                  :text="t('layout.header.dropdownItemLoginOut')"
                  icon="ion:power-outline" />
      </Menu>
    </template>
  </Dropdown>
  <LockAction @register="register" />
  <Modal @register="registerModal" />
</template>
<script lang="ts">
// components
import { Dropdown, Menu } from 'ant-design-vue';

import { defineComponent, computed, ref } from 'vue';

import { DOC_URL } from '/@/settings/siteSetting';

import { userStore } from '/@/store/modules/user';
import { useHeaderSetting } from '/@/hooks/setting/useHeaderSetting';
import { useI18n } from '/@/hooks/web/useI18n';
import { useDesign } from '/@/hooks/web/useDesign';
import { useModal } from '/@/components/Modal';
import headerImg from '/@/assets/images/header.jpg';
import { propTypes } from '/@/utils/propTypes';
import { openWindow } from '/@/utils';

import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
import Modal from './modal.vue';
type MenuEvent = 'logout' | 'doc' | 'lock';

export default defineComponent({
  name: 'UserDropdown',
  components: {
    Modal,
    Dropdown,
    Menu,
    MenuItem: createAsyncComponent(() => import('./DropMenuItem.vue')),
    MenuDivider: Menu.Divider,
    LockAction: createAsyncComponent(() => import('../lock/LockModal.vue')),
  },
  props: {
    theme: propTypes.oneOf(['dark', 'light']),
  },
  setup() {
    const { prefixCls } = useDesign('header-user-dropdown');
    const { t } = useI18n();
    const { getShowDoc } = useHeaderSetting();
    const getUserInfo: any = computed(() => {
      const { userName = '', desc, avatar } = userStore.getUserInfoState || {};

      return { userName, desc, avatar };
    });
    const getUserInfoPhoto: any = computed(() => {
      const { avatar } = userStore.getUserInfoByIdState || {};
      if (avatar) {
        if (avatar.indexOf('http') !== -1) {
          return avatar;
        } else {
          return headerImg;
        }
      } else {
        return headerImg;
      }
    });
    const [register, { openModal: lockOpenModal }] = useModal();
    function handleLock() {
      lockOpenModal(true);
    }
    const [registerModal, { openModal: openEditModal }] = useModal();
    function editPhoto() {
      openEditModal(true);
    }
    //  login out
    function handleLoginOut() {
      userStore.confirmLoginOut();
    }

    // open doc
    function openDoc() {
      openWindow(DOC_URL);
    }

    function handleMenuClick(e: { key: MenuEvent }) {
      switch (e.key) {
        case 'logout':
          handleLoginOut();
          break;
        case 'doc':
          openDoc();
          break;
        case 'lock':
          handleLock();
          break;
      }
    }

    return {
      prefixCls,
      t,
      getUserInfo,
      handleMenuClick,
      getShowDoc,
      register,
      handleLock,
      handleLoginOut,
      registerModal,
      editPhoto,
      getUserInfoPhoto,
    };
  },
});
</script>
<style lang="less">
@prefix-cls: ~'@{namespace}-header-user-dropdown';

.@{prefix-cls} {
  height: @header-height;
  padding: 0 0 0 10px;
  padding-right: 10px;
  overflow: hidden;
  font-size: 12px;
  cursor: pointer;
  align-items: center;

  img {
    width: 24px;
    height: 24px;
    margin-right: 12px;
  }

  &__header {
    border-radius: 50%;
  }

  &__name {
    font-size: 14px;
  }

  &--dark {
    &:hover {
      background: @header-dark-bg-hover-color;
    }
  }

  &--light {
    .@{prefix-cls}__name {
      color: @text-color-base;
    }

    .@{prefix-cls}__desc {
      color: @header-light-desc-color;
    }
  }

  &-dropdown-overlay {
    .ant-dropdown-menu-item {
      min-width: 160px;
    }
  }
}
</style>
